package com.aysafety.iotdataplatformweb.common;

import com.alibaba.fastjson.JSON;
import com.aysafety.iotdataplatformweb.net.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component @ConfigurationProperties("pythonalgorithm") public class PythonAlgorithmUtils {

    String serverUrl = "";

    Map<String, String> serverMap = new HashMap<String, String>();
    private Logger logger = LoggerFactory.getLogger(PythonAlgorithmUtils.class);

    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public Map<String, String> getServerMap() {
        return serverMap;
    }

    public void setServerMap(Map<String, String> serverMap) {
        this.serverMap = serverMap;
    }

    public Map<String, Object> serviceGenetic(Map<String, Object> params) throws IOException {
        Map<String, Object> result = new HashMap<String, Object>();
        if (!serverMap.containsKey("ga")) {
            return result;
        }
        String url = serverMap.get("ga").toString();
        url = this.serverUrl + url;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.put("X-Requested-With", "XMLHttpRequest");
        String requestStr = JSON.toJSONString(params);
        String encode = "utf-8";
        String responseJson = HttpClientUtils
            .convertResponseToString(HttpClientUtils.httpPostFormHttpResponse(url, requestStr, headers, encode),
                encode);
        logger.info("Genetic-url:" + url);
        logger.info("Genetic-params:" + JSON.toJSONString(params));
        logger.info("Genetic-responseJson:" + responseJson);
        logger.info("header: " + headers);
        result = JSON.parseObject(responseJson);
        return result;
    }

    public Map<String, Object> serviceKmeans(Map<String, Object> params) throws IOException {
        Map<String, Object> result = new HashMap<String, Object>();
        if (!serverMap.containsKey("kmeans")) {
            return result;
        }
        String url = serverMap.get("kmeans").toString();
        url = this.serverUrl + url;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.put("X-Requested-With", "XMLHttpRequest");
        String requestStr = JSON.toJSONString(params);
        String encode = "utf-8";
        String responseJson = HttpClientUtils
            .convertResponseToString(HttpClientUtils.httpPostFormHttpResponse(url, requestStr, headers, encode),
                encode);
        logger.debug("kmeans-url:" + url);
        logger.debug("kmeans-params:" + JSON.toJSONString(params));
        logger.debug("kmeans-responseJson:" + responseJson);
        result = JSON.parseObject(responseJson);
        return result;
    }

    public String serviceHermite(Map<String, Object> params) throws IOException {
        Map<String, Object> result = new HashMap<String, Object>();
        if (!serverMap.containsKey("hermite")) {
            return null;
        }
        String url = serverMap.get("hermite").toString();
        url = this.serverUrl + url;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.put("X-Requested-With", "XMLHttpRequest");
        String requestStr = JSON.toJSONString(params);
        System.out.println(requestStr);
        String encode = "utf-8";
        String responseJson = HttpClientUtils
            .convertResponseToString(HttpClientUtils.httpPostFormHttpResponse(url, requestStr, headers, encode),
                encode);
        logger.debug("hermite-url:" + url);
        logger.debug("hermite-params:" + JSON.toJSONString(params));
        logger.debug("hermite-responseJson:" + responseJson);
        //result = JSON.parseObject(responseJson);
        return responseJson;
    }
}
